<?php

declare(strict_types=1);

namespace App\Model\Config;

use Hyperf\DbConnection\Model\Model;

/**
 * @property int $id
 * @property string $name 配置名称
 * @property int $sort  排序
 * @property int $is_show 是否显示1=否 2=是 (如果选择2 那么只能添加一条配置项)
 * @property string $config 配置项
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 */
class Config extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'config';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [
        'config',
        'name',
        'sort',
        'is_menu'
    ];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'sort' => 'integer', 'is_menu' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    public function setConfigAttribute($value): void
    {
        $this->attributes['config'] = json_encode($value);
    }

    public function getConfigAttribute($value): array
    {
        return json_decode($value, true);
    }
}
